/**
 * 
 */
package de.cbf.cam.dsa.model.entities.items;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;

import de.cbf.cam.dsa.model.entities.BattleChar;
import de.cbf.cam.dsa.model.entities.OneNPC;
import de.cbf.cam.dsa.model.entities.states.stateChanger.StateChangerBase;
import de.cbf.cam.dsa.model.interfaces.IUsableItemBattle;
import de.cbf.cam.dsa.model.interfaces.IUsableItemCharacter;

/**
 * @author Nebelritter
 *
 */
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Elixir extends Item implements IUsableItemBattle,IUsableItemCharacter {
	
	/**
	 * @uml.property  name="stateChangers"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.cbf.cam.dsa.model.entities.states.stateChanger.StateChangerBase"
	 */
	private List<StateChangerBase> stateChangers = new ArrayList<StateChangerBase>();
	
	
	/**
	 * @uml.property  name="propertyChangeAdvance"
	 * @uml.associationEnd  multiplicity="(1 1)" inverse="this$0:de.cbf.cam.dsa.model.entities.items.Elixir$PropertyChangeAdvance"
	 */
	private PropertyChangeAdvance propertyChangeAdvance = new PropertyChangeAdvance();
	/**
	 * 
	 */
	public Elixir() {
		
	}
	/**
	 * @param name
	 * @param description
	 */
	public Elixir(String name,String place) {
		super(name, place);
	}
	@Override
	public void useOnCharacter(OneNPC character) {
		for (Iterator<StateChangerBase> iterator = stateChangers.iterator(); iterator.hasNext();) {
			StateChangerBase stateChanger = (StateChangerBase) iterator.next();
			stateChanger.changeState(character);
		}
	}
	@Override
	public void useOnCharacterInBattle(BattleChar battleChar) {
		for (Iterator<StateChangerBase> iterator = stateChangers.iterator(); iterator.hasNext();) {
			StateChangerBase stateChanger = (StateChangerBase) iterator.next();
			stateChanger.changeStateBattle(battleChar);
		}
	}
	@ManyToMany
	public List<StateChangerBase> getStateChangers() {
		return stateChangers;
	}
	/**
	 * @param stateChangers the stateChangers to set
	 */
	protected void setStateChangers(List<StateChangerBase> stateChangers) {
		this.stateChangers = stateChangers;
	}
	public void addStateChanger(StateChangerBase stateChanger){
		stateChanger.propertyChangeSupport.addPropertyChangeListener(propertyChangeAdvance);
		this.stateChangers.add(stateChanger);
		propertyChangeSupport.firePropertyChange(PROPERTYNAME+"stateChangers", null, stateChangers);
	}
	public void removeStateChanger(StateChangerBase stateChanger){
		stateChanger.propertyChangeSupport.removePropertyChangeListener(propertyChangeAdvance);
		this.stateChangers.remove(stateChanger);
		propertyChangeSupport.firePropertyChange(PROPERTYNAME+"stateChangers", null, stateChangers);
	}
	
	private class PropertyChangeAdvance implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			propertyChangeSupport.firePropertyChange(PROPERTYNAME+evt.getPropertyName(), null,Elixir.this);			
		}	
	}

	
}
